<?php
namespace App\Components;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/**
 * 生成二维码工具类
 */
class SimpleQrcode{
  public $simple_qrcode = '/data/cdn_root';
  
  /**
   * 生成大会渠道二维码
   */
  public function meetingChannel($url,$m_id,$c_id,$type='valid'){
    $name = $type.'_'.$m_id.'_'.$c_id.'.png';
    $path = '/simple_qrcode/meeting/';
    $cdn_path = $this->simple_qrcode.$path;
    $this->createFolder($cdn_path);
    $simpleQrcodeImgFile = $cdn_path.$name;
    if(!file_exists($simpleQrcodeImgFile)){
      QrCode::format('png')->size(318)->generate($url,$simpleQrcodeImgFile);
    }
    $result['size'] = getimagesize($simpleQrcodeImgFile);
    $result['url'] = env('FILE_DOMAIN').$path.$name;
    return $result;
  }
  
  /**
   * 生成大会渠道二维码
   */
  public function meetingChannelV2($url,$m_id,$c_id,$type='valid'){
    $name = $type.'_'.$m_id.'_'.$c_id.'.png';
    $path = '/simple_qrcode/meetingv2/';
    $cdn_path = $this->simple_qrcode.$path;
    $this->createFolder($cdn_path);
    $simpleQrcodeImgFile = $cdn_path.$name;
    if(!file_exists($simpleQrcodeImgFile)){
      QrCode::format('png')->size(318)->generate($url,$simpleQrcodeImgFile);
    }
    $result['size'] = getimagesize($simpleQrcodeImgFile);
    $result['url'] = env('FILE_DOMAIN').$path.$name;
    return $result;
  }
  
  /**
   * 生成商城渠道二维码
   */
  public function shoppingChannel($url,$g_id,$c_id,$type='valid'){
    $name = $type.'_'.$g_id.'_'.$c_id.'.png';
    $path = '/simple_qrcode/shopping/';
    $cdn_path = $this->simple_qrcode.$path;
    $this->createFolder($cdn_path);
    $simpleQrcodeImgFile = $cdn_path.$name;
    if(!file_exists($simpleQrcodeImgFile)){
      QrCode::format('png')->size(318)->generate($url,$simpleQrcodeImgFile);
    }
    $result['size'] = getimagesize($simpleQrcodeImgFile);
    $result['url'] = env('FILE_DOMAIN').$path.$name;
    return $result;
  }
  
  private function createFolder($path) {
    if (!file_exists($path)) {
      $this->createFolder(dirname($path));
      mkdir($path);
      chmod($path, 0777);
    }
    return true;
  }
}
